<?php

if ($_REQUEST['path'])
{
    $imgInfo->imagePath = $_REQUEST['path'];
    $imgInfo->quality   = is_numeric($_REQUEST['quality']) ? $_REQUEST['quality'] : 75;
    //$imgInfo->imagePath = str_replace('"','', $imgInfo->imagePath );
    $imgInfo->imagePath = str_replace("%27",'', $imgInfo->imagePath );
    $imgInfo->newWidth  = is_numeric( $_REQUEST['width'] )  ? $_REQUEST['width']: null;
    $imgInfo->newHeight = is_numeric( $_REQUEST['height'] ) ? $_REQUEST['height'] : null;
    $imgInfo->antiAlias  = $_REQUEST['antialias'] == 'false' ? false : true ; //padr�o true
    $imgInfo->type      = $_REQUEST['type'];

    $getImage = new getImage( $imgInfo );
    $getImage->__toString();
}

class getImage
{
    public $imgInfo;

    public function __construct( $imgInfo )
    {
        $this->imgInfo = $imgInfo;
    }

    public function __toString()
    {
        $imgInfo = $this->imgInfo;

        if ( file_exists($imgInfo->imagePath) )
        {
            $imageSize = getimagesize($imgInfo->imagePath);

            $imgInfo->width     = $imageSize[0];
            $imgInfo->height    = $imageSize[1];
            $imgInfo->mime      = $imageSize['mime'];
            $imgInfo->bits      = $imageSize['bits'];
	        $thumb 	= null;
	        $source = null;
	
            if ($imgInfo->newWidth && !$imgInfo->newHeight)
            {
                $percent =   $imgInfo->newWidth / $imgInfo->width;
                $imgInfo->newHeight = $imgInfo->height*$percent;
            }

            if ($imgInfo->newHeight && !$imgInfo->newWidth)
            {
                $percent =   $imgInfo->newHeight / $imgInfo->height;
                $imgInfo->newWidth = $imgInfo->width*$percent;
            }

            if ($imgInfo->newWidth && $imgInfo->newHeight)
            {
		        if ( $imgInfo->mime == 'image/jpeg' )
		        {
			        $source = imagecreatefromjpeg($imgInfo->imagePath);
			        $thumb  = imagecreatetruecolor( $imgInfo->newWidth, $imgInfo->newHeight );
                    if ($imgInfo->antiAlias == false)
                    {
			            imagecopyresized($thumb, $source, 0, 0, 0, 0, $imgInfo->newWidth, $imgInfo->newHeight, $imgInfo->width, $imgInfo->height );
                    }
                    else
                    {
                        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $imgInfo->newWidth, $imgInfo->newHeight, $imgInfo->width, $imgInfo->height );
                    }
                    header("Content-type: {$imageSize['mime']}");
                    header("Content-Disposition: inline; filename={$imgInfo->imagePath}"); //se por attachament ele abre o download
                    imagejpeg($thumb, null, $imgInfo->quality);
		        }
			
                if ( $imgInfo->mime == 'image/png' )
                {
		            $source = imagecreatefrompng($imgInfo->imagePath);
			        $imgInfo->trueColor = imageistruecolor($source);

                    if ( $imgInfo->trueColor)
                    {
                        $thumb  = imagecreatetruecolor( $imgInfo->newWidth, $imgInfo->newHeight );
                        imagealphablending($thumb, false);
                        imagesavealpha  ( $thumb  , true );

                        if ( $imgInfo->type == 'indexed')
                        {
                            imagetruecolortopalette  ( $source  ,  true , 256 );
                        }
                    }
                    else
                    {
                        $thumb  = imagecreate( $imgInfo->newWidth, $imgInfo->newHeight );
                        imagealphablending( $thumb, false );
                        $transparent = imagecolorallocatealpha( $thumb, 0, 0, 0, 127 );
                        imagefill( $thumb, 0, 0, $transparent );
                        imagesavealpha( $thumb,true );
                        imagealphablending( $thumb, true );
                    }

                    if ($imgInfo->antiAlias == false)
                    {
			            imagecopyresized($thumb, $source, 0, 0, 0, 0, $imgInfo->newWidth, $imgInfo->newHeight, $imgInfo->width, $imgInfo->height );
                    }
                    else
                    {
                        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $imgInfo->newWidth, $imgInfo->newHeight, $imgInfo->width, $imgInfo->height );
                    }

                    header("Content-Type: {$imageSize['mime']}");
                    imagepng($thumb);
                }
            }
            else
            {
		        //aqui vai otimiza��o se n�o quiser redimensionar
                $fp = fopen($imgInfo->imagePath, "rb");
                if ($imageSize && $fp)
                {
                    header("Content-type: {$imageSize['mime']}");
                    header("Content-Length: ".filesize($imgInfo->imagePath));
                    fpassthru($fp);
                    exit;
                }
            }
	        if ($thumb)   imagedestroy($thumb);
            if ($source)  imagedestroy($source);
        }
        else
        {
            echo 'image not find.';
        }
    }
}
?>
